package io.renren.modules.sp.controller.front;

import io.renren.common.interceptor.UserContext;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Result;
import io.renren.modules.sp.dto.*;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpNoticeEntity;
import io.renren.modules.sp.entity.SpNoticeLogEntity;
import io.renren.modules.sp.service.SpMatchItemService;
import io.renren.modules.sp.service.SpMatchService;
import io.renren.modules.sp.service.SpNoticeLogService;
import io.renren.modules.sp.service.SpNoticeService;
import io.renren.modules.sqlapi.service.SysApiSqlService;
import io.renren.modules.sys.dao.SysCommDao;
import io.renren.modules.sys.dao.SysParamsDao;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-04-20
*/
@RestController
@RequestMapping("front/notice")
public class FrontNoticeController extends FrontBaseController{
    @Autowired
    private SysCommDao sysCommDao;
    @Autowired
    private SpNoticeService spNoticeService;
    @Autowired
    private SpNoticeLogService spNoticeLogService;
    @Autowired
    private SysApiSqlService sysApiSqlService;

    @GetMapping("page")
    public Result<PageData<Map>> page(HttpServletRequest request, @RequestParam Map<String, Object> params){
        // 取当前登录用户
        SpUserDTO user = this.getFrontUserInfo(request);
        params.put("userId", user.getId());
        String roleId =  user.getRoleId();
        //根据逗号分割
        String[] roleIds = roleId.split(",");
        String sql = "";
        //循环比对
        for (String role : roleIds) {
            sql += " or t.role_id like '%" + role + "%'";
        }
        sql = "(" + sql.replaceFirst("or", "") + ")";
        params.put("sqlFilter", sql);
        PageData<Map> pageData = spNoticeService.pageNotice(params);
        return new Result<PageData<Map>>().ok(pageData);
    }

    @GetMapping("detail/{id}")
    public Result<SpNoticeEntity> detail(@PathVariable("id") Long id, HttpServletRequest request) {
        // 取当前登录用户
        SpUserDTO user = this.getFrontUserInfo(request);

        SpNoticeEntity spNoticeEntity = spNoticeService.selectById(id);
        //保存日志
        SpNoticeLogDTO spNoticeLogDTO = new SpNoticeLogDTO();
        spNoticeLogDTO.setNoticeId(id);
        spNoticeLogDTO.setUserId(user.getId());
        spNoticeLogDTO.setCreateDate(new Date());
        spNoticeLogService.save(spNoticeLogDTO);
        return new Result<SpNoticeEntity>().ok(spNoticeEntity);
    }

    @GetMapping("unReadCount")
    public Result<String> unReadCount(HttpServletRequest request){
        Map<String, Object> params = new HashMap<>();
        // 取当前登录用户
        SpUserDTO user = this.getFrontUserInfo(request);
        params.put("userId", user.getId());
        String roleId =  user.getRoleId();
        //根据逗号分割
        String[] roleIds = roleId.split(",");
        String sql = "";
        //循环比对
        for (String role : roleIds) {
            sql += " or t.role_id like '%" + role + "%'";
        }
        sql = "(" + sql.replaceFirst("or", "") + ")";
        params.put("sqlFilter", sql);
        Integer count = spNoticeService.unReadCount(params);
        return new Result<String>().ok(count.toString());
    }


    @GetMapping("readItemCount")
    public Result<Map<String,Object>> readItemCount(Long matchItemId, Long userId,HttpServletRequest request){
        Integer count = spNoticeService.readItemCount(matchItemId, userId);
        Map<String,Object> map = new HashMap<>();
        map.put("count", count);
        map.put("userId", userId);
        return new Result<Map<String,Object>>().ok(map);
    }

    /**
     * 保存赛项通知浏览记录
     * @param matchItemId
     * @param userId
     * @return
     */
    @GetMapping("saveItemNoticeLog")
    public Result<String> readItem(Long matchItemId, Long userId){
        //增加日志
        Map<String, Object> param = new HashMap();
        String sql = "insert into sp_notice_item_log (user_id, item_id) values (#{p.userId}, #{p.itemId})";
        param.put("userId", userId);
        param.put("itemId", matchItemId);
        sysCommDao.query(sql, param);
        return new Result<String>().ok("");
    }

    /**
     * 全部已读
     */
    @GetMapping("readAll")
    public Result<String> readAll(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", 1);
        params.put("page", 1);
        params.put("pageSize", 1000);
        params.put("user", UserContext.getUser());
        List<Map<String, String>> list = sysApiSqlService.list("notice", params);
        for (Map<String, String> map : list) {
            // 未读的保存日志
            Object userId = map.get("user_id");
            if(userId == null){
                Object noticeId = (Object) map.get("id");
                SpNoticeLogDTO spNoticeLogDTO = new SpNoticeLogDTO();
                spNoticeLogDTO.setNoticeId(Long.parseLong(noticeId.toString()));
                spNoticeLogDTO.setUserId(UserContext.getUser().getId());
                spNoticeLogDTO.setCreateDate(new Date());
                spNoticeLogService.save(spNoticeLogDTO);
            }
        }
        return new Result<String>().ok("");
    }



}